#include "oal/Device.h"
#include "wtclog/Logger.h"

using namespace Oal;

Oal::Device::Device()
{
	m_Desc.m_Attrlist = 0;
	m_Desc.m_DeviceName = 0;

	Init();
}

Device::Device(const Desc& desc)
	:m_Desc(desc)
{
	Init();
}

bool Device::Init()
{
	m_Device = alcOpenDevice(m_Desc.m_DeviceName);
	if (!m_Device)
	{
		wtclog::error("cannot open device");
		return false;
	}

	m_Context = alcCreateContext(m_Device, m_Desc.m_Attrlist);
	
	if (!alcMakeContextCurrent(m_Context))
	{
		wtclog::error("cannot make context current");
		return false;
	}
	wtclog::info("success init openal environment");
	
	return true;
}

void Device::Destory()
{
	alcMakeContextCurrent(nullptr);
	alcDestroyContext(m_Context);
	alcCloseDevice(m_Device);
}

Oal::Device::~Device()
{
	Destory();
}
